package org.example.baseinfo.common.vo;


public class BaseResponse<T> {

    public static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";

    public static final String DEFAULT_ERROR_MESSAGE = "系统异常";

    public static final String DEFAULT_SUCCESS_CODE = "200";

    public static final String DEFAULT_ERROR_CODE = "500";

    private String code;

    private String msg;

    private T data;

    public BaseResponse() {
    }

    public BaseResponse(String code, String msg, T data) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static <T> BaseResponse<T> success(T t) {
        BaseResponse<T> res = new BaseResponse<>();
        res.setCode(DEFAULT_SUCCESS_CODE);
        res.setMsg(DEFAULT_SUCCESS_MESSAGE);
        res.setData(t);
        return res;
    }

    public static <T> BaseResponse<T> success() {
        BaseResponse<T> res = new BaseResponse<>();
        res.setCode(DEFAULT_SUCCESS_CODE);
        res.setMsg(DEFAULT_SUCCESS_MESSAGE);
        res.setData(null);
        return res;
    }

    public static <T> BaseResponse<T> error(IError error) {
        BaseResponse<T> res = new BaseResponse<>();
        res.setCode(error.getCode());
        res.setMsg(error.getMsg());
        res.setData(null);
        return res;
    }

    public static <T> BaseResponse<T> error(String msg) {
        BaseResponse<T> res = new BaseResponse<>();
        res.setCode(IError.GENERAL_ERROR.getCode());
        res.setMsg(msg);
        res.setData(null);
        return res;
    }

    public static <T> BaseResponse<T> error(IError error, String msg) {
        BaseResponse<T> res = new BaseResponse<>();
        res.setCode(error.getCode());
        res.setMsg(msg);
        res.setData(null);
        return res;
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
